package org.miyuki4j.cfgtool.type;

import org.miyuki4j.cfgtool.*;
import org.miyuki4j.cfgtool.exception.TypeInstanceException;
import org.miyuki4j.cfgtool.exception.TypeParseException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/24/星期四 14:40
 * @Version 1.0
 */
public class DateType implements Type.SimpleType<LocalDateTime> {

    public static final String FORMAT_TEMPLATE = "yyyy-MM-dd HH:mm:ss";

    @Override
    public String name() {
        return "date";
    }

    @Override
    public String javaInterface() {
        return "java.time.LocalDateTime";
    }

    @Override
    public String defaultValue() {
        return "null";
    }

    @Override
    public LocalDateTime instance(String value, String delimiters) {
        try {
            return LocalDateTime.parse(value, DateTimeFormatter.ofPattern(delimiters));
        } catch (NumberFormatException nfe) {
            throw new TypeInstanceException("parse date value:" + value, nfe);
        }
    }

    @Override
    public void parse(TableField field, String type, String delimiters) {
        SimpleType.super.parse(field, type, delimiters);
        try {
            DateTimeFormatter.ofPattern(delimiters);
        } catch (Exception e) {
            throw new TypeParseException("datetime format error,  delimiters:" + delimiters);
        }
    }
}
